package hu.bme.mit.androtext.age.ui.action;

import hu.bme.mit.androtext.generator.AppGenerator;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

public class GenerateAndroidApplicationAction implements IObjectActionDelegate {

	/**
	 * The model file that is a source for the application generation.
	 */
	private IFile source;

	@Override
	public void run(IAction action) {
		System.out.println("Running Generate Android Application Action");
		generateApp();
		System.out.println("Finishing Generate Android Application Action");
	}

	private void generateApp() {
		Job j = new Job("Generate Android App") {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				System.out.println("Generate Android Application Started");
				IStatus status = Status.OK_STATUS;
				monitor.beginTask("Generate Android Project", 1000);
				try {
					AppGenerator gen = new AppGenerator(getSource());
					gen.generate(monitor);
					//descriptor.unload();
				} catch (OperationCanceledException e) {
					status = Status.CANCEL_STATUS;
				} catch (Exception e) {
					e.printStackTrace();
					status = new Status(IStatus.ERROR, "hu.bme.mit.androtext.age.ui", e.toString() + ": " +e.getMessage());
				} finally {
					monitor.done();
				}
				System.out.println("Generate Android Application Finished");
				return status;
			}
		};
		j.addJobChangeListener(new JobChangeAdapter() {
			@Override
			public void done(IJobChangeEvent event) {
				showDialog(event);
			}
		});
		j.setUser(true);
		j.schedule();
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			Object o = ((IStructuredSelection) selection).getFirstElement();
			if (o instanceof IFile) {
				setSource((IFile) o);
			}
		}
	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
	}

	public void setSource(IFile source) {
		this.source = source;
	}

	public IFile getSource() {
		return source;
	}
	
	/**
	 * Shows the proper dialog based on the actual IStatus.
	 * @param status
	 */
	protected void showDialog(final IJobChangeEvent event) {
		Display display = Display.getDefault();
		display.asyncExec(new Runnable() {
			@Override
			public void run() {
				Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
				switch (event.getResult().getSeverity()) {
				case IStatus.ERROR:
					ErrorDialog.openError(shell, "Code Generation Error", null, event.getResult());
					break;
				case IStatus.CANCEL:
					MessageDialog.openInformation(shell, "Code Generation Canceled", event.getJob().getName() + "Code generation canceled!");
					break;
				default:
					MessageDialog.openInformation(shell, "Code generation finished!", event.getJob().getName()+" finished without errors!");
					break;
				}
			}
			
		});
	}


}
